﻿using diary.POCO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace diary
{
    /// <summary>
    /// Interaction logic for DayView.xaml
    /// </summary>
    public partial class DayView : UserControl
    {
        public delegate void SaveCommandHandler(object sender);
        public event SaveCommandHandler OnSaveCommand;
        private Day _day;

        public DayView()
        {
            InitializeComponent();
        }

        public void SetDay(Day day)
        {
            _day = day;
            DateTime yesterday = DateTime.Now.AddDays(-1);
            if (day.CurrentDate.Year == DateTime.Now.Year
                && day.CurrentDate.Month == DateTime.Now.Month
                && day.CurrentDate.Day == DateTime.Now.Day)
            {
                CurrentDate.Content = "Today - " + String.Format("{0:dddd, MMMM d, yyyy}", day.CurrentDate);
            }
            else if (day.CurrentDate.Year == yesterday.Year
                && day.CurrentDate.Month == yesterday.Month
                && day.CurrentDate.Day == yesterday.Day)
            {
                CurrentDate.Content = "Yesterday - " + String.Format("{0:dddd, MMMM d, yyyy}", day.CurrentDate);
            }
            else
            {
                CurrentDate.Content = String.Format("{0:dddd, MMMM d, yyyy}", day.CurrentDate);
            }

            
            if (day.Entry!=null)
            {
                Entry.Text = day.Entry.Content;
            }

            // BOOKS
            if (day.Books.Count > 0)
            {
                Book1.Book = day.Books[0];
            }
            else
            {
                Book1.Book = new Book();
            }

            // MOVIES
            if (day.Movies.Count > 0)
            {
                Movies.Movie = day.Movies[0];
            }
            else
            {
                Movies.Movie = new Movie();
            }

            // ALBUMS
            if (day.Albums.Count > 0)
            {
                Album1.Album = day.Albums[0];
            }
            else
            {
                Album1.Album = new Album();
            }
            if (day.Albums.Count > 1)
            {
                Album2.Album = day.Albums[1];
            }
            else
            {
                Album2.Album = new Album();
            }

            // WORKOUTS
            if (day.Workouts.Count > 0)
            {
                Workouts.Text = day.Workouts[0].Title;
            }

            // TELEVISION
            if (day.Television.Count > 0)
            {
                TV1.Television = day.Television[0];
                if (day.Television.Count > 1)
                {
                    TV2.Television = day.Television[1];
                }
                else
                {
                    TV2.Television = new Television();
                }
            }
            else
            {
                TV1.Television = new Television();
                TV2.Television = new Television();
            }

            // EVENTS
            if (day.Events.Count > 0)
            {
                Event1.Event = day.Events[0];
                if (day.Events.Count > 1)
                {
                    Event2.Event = day.Events[1];
                }
                else
                {
                    Event2.Event = new Event();
                }
            }
            else
            {
                Event1.Event = new Event();
                Event2.Event = new Event();
            }

            // WORK
            if (day.Work!=null)
            {
                Work.Text = day.Work.Content;
            }

            // PICTURES
            if (day.Pictures != null)
            {
                foreach(Picture pic in day.Pictures)
                {
                    ListBoxItem item = new ListBoxItem();
                    item.Content = pic.File;
                    Pics.Items.Add(item);
                }
            }
        }

        private void ButtonAddPic_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog 
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".png";
            dlg.Filter = "JPG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif|JPEG Files (*.jpeg)|*.jpeg";

            // Display OpenFileDialog by calling ShowDialog method 
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox 
            if (result == true)
            {
                // Open document 
                string filename = dlg.FileName;
                ListBoxItem item = new ListBoxItem();
                item.Content = filename;
                Pics.Items.Add(item);
            }
        }

        private void delete_click(object sender, RoutedEventArgs e)
        {
            Pics.Items.Remove(Pics.SelectedItem);
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
             _day.Entry.Content = Entry.Text;
             _day.Work.Content = Work.Text;


            // WORKOUTS
             _day.Workouts.Clear();
            if (string.IsNullOrEmpty(Workouts.Text) == false)
            {
                Workout workout = new Workout();
                workout.Title = Workouts.Text;
                _day.Workouts.Add(workout);
            }

            // MOVIES
            _day.Movies.Clear();
            if (string.IsNullOrEmpty(Movies.Movie.Title) == false)
            {
                _day.Movies.Add(Movies.Movie);
            }

            // BOOKS
            _day.Books.Clear();
            if (string.IsNullOrEmpty(Book1.Book.Title) == false)
            {
                _day.Books.Add(Book1.Book);
            }

            // ALBUMS
            _day.Albums.Clear();
            if (string.IsNullOrEmpty(Album1.Album.Title) == false)
            {
                _day.Albums.Add(Album1.Album);
                if (string.IsNullOrEmpty(Album2.Album.Title) == false)
                {
                    _day.Albums.Add(Album2.Album);
                }
            }

            // EVENTS
            _day.Events.Clear();
            if (string.IsNullOrEmpty(Event1.Event.Content) == false)
            {
                _day.Events.Add(Event1.Event);
            }
            if (string.IsNullOrEmpty(Event2.Event.Content) == false)
            {
                _day.Events.Add(Event2.Event);
            }

            // TELEVISION
            _day.Television.Clear();
            if (string.IsNullOrEmpty(TV1.Television.Title) == false)
            {
                _day.Television.Add(TV1.Television);
            }
            if (string.IsNullOrEmpty(TV2.Television.Title) == false)
            {
                _day.Television.Add(TV2.Television);
            }

            // PICTURES
            _day.Pictures.Clear();
            if (Pics.Items.Count > 0)
            {
                foreach (ListBoxItem item in Pics.Items)
                {
                    Picture pic = new Picture();
                    pic.File = item.Content.ToString();
                    _day.Pictures.Add(pic);
                }
            }

            OnSaveCommand(this);
        }
    }
}
